using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Entities;
using System.Configuration;

namespace DAL
{
   public  class tbCTDonHangDAL
    {
       private SqlConnection cn = null;
        public tbCTDonHangDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }

        public tbCTDonHangDAL(tbCTDonHangDAL dao)
        {
            this.cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            this.cn.Open();
        }
       public void Insert(tbCTDonHang ctdh)
       {
           SqlCommand cm = new SqlCommand("tbCTDonHang_Insert", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@SoDH", ctdh.SoDH);
           cm.Parameters.AddWithValue("@MaSP", ctdh .MaSP);
           cm.Parameters.AddWithValue("@GiaBan", ctdh.GiaBan);
           cm.Parameters.AddWithValue("@Soluong", ctdh .Soluong);           
           cm.ExecuteNonQuery();
       }
       public void Update(tbCTDonHang ctdh)
       {
           SqlCommand cm = new SqlCommand("tbCTDonHang_Update", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@MaCTHD", ctdh.MaCTHD);
           cm.Parameters.AddWithValue("@SoDH", ctdh.SoDH);
           cm.Parameters.AddWithValue("@MaSP", ctdh.MaSP);
           cm.Parameters.AddWithValue("@GiaBan", ctdh.GiaBan);
           cm.Parameters.AddWithValue("@Soluong", ctdh.Soluong);      
           cm.ExecuteNonQuery();
       }
       public void Delete(int ID)
       {
           SqlCommand cm = new SqlCommand("tbCTDonHang_Detele", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@MaCTHD", ID);
           cm.ExecuteNonQuery();

       }
       public tbCTDonHangCollection Get(string where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
       {
           SqlCommand cm = new SqlCommand("tbCTDonHang_GET", cn);
           cm.CommandType = CommandType.StoredProcedure;
           cm.Parameters.AddWithValue("@Where", where);
           cm.Parameters.AddWithValue("@PageIndex", pageIndex);
           cm.Parameters.AddWithValue("@PageSize", pageSize);
           cm.Parameters.AddWithValue("@OrderBy", orderBy);
           cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
           cm.Parameters.Add(new SqlParameter("@TotalRecords", DbType.Int32)).Direction = ParameterDirection.Output;
           SqlDataReader dr = cm.ExecuteReader();
          tbCTDonHangCollection CTDonHangCollection = new tbCTDonHangCollection();
           while (dr.Read())
           {
               tbCTDonHang CTDonHang = new tbCTDonHang();
               CTDonHang .MaCTHD  = int.Parse(dr.GetValue(0).ToString());
               CTDonHang .SoDH  = int.Parse(dr.GetValue(1).ToString());               
               CTDonHang .MaSP  = int.Parse(dr.GetValue(2).ToString());              
               CTDonHang .GiaBan  = int.Parse(dr.GetValue(3).ToString());
               CTDonHang .Soluong  = int.Parse(dr.GetValue(4).ToString());               
               CTDonHangCollection.Add(CTDonHang);
           }
           dr.Close();
           totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
           return CTDonHangCollection;


       }
    }
}
